﻿namespace HBFramework
{
    public abstract class Singleton<T> where T : Singleton<T>, ISingleton, new()
    {
        private static T _Instance;
        public static T Instance
        {
            get
            {
                if (_Instance != null)
                    return _Instance;
                else
                {
                    UnityEngine.Debug.LogError($"[{typeof(T)}]类使用前需要通过SingleontManager注册");
                    return null;
                }
            }
        }

        protected Singleton()
        {
            if (_Instance != null)
            {
                UnityEngine.Debug.LogError("[Singleton] 单例类已经注册过了");
                return;
            }
            _Instance = this as T;
        }

        /// <summary>
        /// 销毁实例
        /// </summary>
        protected void Destroy()
        {
            _Instance.OnDestroy();
            _Instance = null;
        }
    }
}